package CAPA_INTERFAZ_DE_USUARIO;

import CAPA_LOGICA_DE_NEGOCIO.LN_LOGICA_NEGOCIO;
import RECURSOS.CLIENTE;
import RECURSOS.COMPRA;
import RECURSOS.ERROR;
import RECURSOS.FACTURA;
import RECURSOS.FONDO_FORM;
import RECURSOS.LOG;
import RECURSOS.PRODUCTO;
import java.awt.Image;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.view.JasperViewer;


public class IU_FACTURA extends javax.swing.JFrame {
    
    private LN_LOGICA_NEGOCIO obj_ln;
    private CLIENTE objcli;
    private COMPRA objcomp;
    private FACTURA objfac;
    
    private static DefaultTableModel modelofac=new DefaultTableModel(){public boolean isCellEditable(int row, int column) {
            return false;
        }};
   
    private int ivaporc;
    private float descuento;
    private Date fecha;
    private String fechafac;
    private FONDO_FORM F;
    
    
    @Override    
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("RECURSOS/SOVI.PNG"));
        return retValue;
    }

    public IU_FACTURA() {       
        initComponents();
        
        F=new FONDO_FORM(this.getWidth(),this.getHeight(),"fondo.png");                       
        this.add(F);
        this.pack();                
        this.setLocationRelativeTo(null);
        
        fecha = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");        
        this.lblFecha.setText(sd.format(fecha));        
        
        descuento=0;
        
        obj_ln=new LN_LOGICA_NEGOCIO();
        objcli=new CLIENTE();
        objcomp = new COMPRA();    
        objfac=new FACTURA();
        
        txtCedula.setInputVerifier(new Verificar_CedulaRuc());        
        //modelofac=new DefaultTableModel();
        Object[] etiquetas = new Object[5];                              
        etiquetas[0] = "CODIGO";                    
        etiquetas[1] = "CANTIDAD";                   
        etiquetas[2] = "DESCRIPCION";                        
        etiquetas[3] = "PRECIO UNITARIO";
        etiquetas[4] = "PRECIO TOTAL";
        modelofac.setColumnIdentifiers(etiquetas);
        this.TablaProductos.setModel(modelofac);
        ivaporc=12;                
        this.txtDescuento.setInputVerifier(new Verificar_solo_Decimal());                        
    }

    public static DefaultTableModel getModelofac() {
        return modelofac;
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        lblFecha = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCedula = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDireccion = new javax.swing.JTextField();
        btnBuscarCliente = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lbliva = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        txtIva = new javax.swing.JTextField();
        txtDescuento = new javax.swing.JTextField();
        txtSubtotal = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        btncalcular = new javax.swing.JButton();
        brnAgregar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaProductos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SOVI");
        setIconImage(getIconImage());
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jButton2.setForeground(new java.awt.Color(0, 0, 102));
        jButton2.setText("CANCELAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jButton4.setForeground(new java.awt.Color(0, 0, 102));
        jButton4.setText("IMPRIMIR");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 0, 102));
        jLabel4.setText("FECHA EMISION: ");

        lblFecha.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lblFecha.setForeground(new java.awt.Color(0, 0, 102));
        lblFecha.setText("jLabel11");

        jLabel2.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 102));
        jLabel2.setText("CEDULA / RUC :");

        txtCedula.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtCedula.setForeground(new java.awt.Color(0, 0, 102));
        txtCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCedulaKeyTyped(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 102));
        jLabel1.setText("NOMBRES : ");

        txtNombres.setEditable(false);
        txtNombres.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtNombres.setForeground(new java.awt.Color(0, 0, 102));

        jLabel3.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 102));
        jLabel3.setText("DIRECCIÓN :");

        txtDireccion.setEditable(false);
        txtDireccion.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtDireccion.setForeground(new java.awt.Color(0, 0, 102));

        btnBuscarCliente.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btnBuscarCliente.setForeground(new java.awt.Color(0, 0, 102));
        btnBuscarCliente.setText("BUSCAR");
        btnBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarClienteActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 0, 102));
        jLabel6.setText("TELÉFONO: ");

        txtTelefono.setEditable(false);
        txtTelefono.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtTelefono.setForeground(new java.awt.Color(0, 0, 102));

        jLabel5.setFont(new java.awt.Font("Cambria", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 0, 102));
        jLabel5.setText("Datos de la Factura");

        jLabel11.setFont(new java.awt.Font("Cambria", 1, 14)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(0, 0, 102));
        jLabel11.setText("Detalles de la Compra");

        jLabel7.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 0, 102));
        jLabel7.setText("Subtotal : ");

        jLabel8.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 0, 102));
        jLabel8.setText("Descuento : ");

        lbliva.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbliva.setForeground(new java.awt.Color(0, 0, 102));
        lbliva.setText("IVA 12%: ");

        jLabel10.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(0, 0, 102));
        jLabel10.setText("Total a Pagar :");

        txtTotal.setEditable(false);
        txtTotal.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtTotal.setForeground(new java.awt.Color(0, 0, 102));

        txtIva.setEditable(false);
        txtIva.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtIva.setForeground(new java.awt.Color(0, 0, 102));

        txtDescuento.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtDescuento.setForeground(new java.awt.Color(0, 0, 102));
        txtDescuento.setText("0");
        txtDescuento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDescuentoActionPerformed(evt);
            }
        });

        txtSubtotal.setEditable(false);
        txtSubtotal.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtSubtotal.setForeground(new java.awt.Color(0, 0, 102));

        jButton1.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jButton1.setForeground(new java.awt.Color(0, 0, 102));
        jButton1.setText("Cambiar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btncalcular.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btncalcular.setForeground(new java.awt.Color(0, 0, 102));
        btncalcular.setText("Calcular");
        btncalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncalcularActionPerformed(evt);
            }
        });

        brnAgregar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        brnAgregar.setForeground(new java.awt.Color(0, 0, 102));
        brnAgregar.setText("Agregar");
        brnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brnAgregarActionPerformed(evt);
            }
        });

        TablaProductos.setFont(new java.awt.Font("Cambria", 0, 12)); // NOI18N
        TablaProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(TablaProductos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel4)
                        .addGap(6, 6, 6)
                        .addComponent(lblFecha))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBuscarCliente))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel11))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 775, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(brnAgregar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(573, 573, 573)
                        .addComponent(jLabel7)
                        .addGap(57, 57, 57)
                        .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(573, 573, 573)
                        .addComponent(jLabel8)
                        .addGap(46, 46, 46)
                        .addComponent(txtDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(573, 573, 573)
                        .addComponent(lbliva)
                        .addGap(59, 59, 59)
                        .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(573, 573, 573)
                        .addComponent(jLabel10)
                        .addGap(34, 34, 34)
                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(btncalcular))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(342, 342, 342)
                        .addComponent(jButton4)
                        .addGap(199, 199, 199)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(29, 29, 29)
                                .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 525, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(33, 33, 33)
                                .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addComponent(jLabel6)
                                .addGap(10, 10, 10)
                                .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel5)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel4))
                    .addComponent(lblFecha))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnBuscarCliente))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel3))
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(brnAgregar))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel7))
                    .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel8))
                    .addComponent(txtDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(lbliva))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel10))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btncalcular))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jButton2))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jLabel4.getAccessibleContext().setAccessibleParent(this);
        lblFecha.getAccessibleContext().setAccessibleParent(this);
        jLabel2.getAccessibleContext().setAccessibleParent(this);
        txtCedula.getAccessibleContext().setAccessibleParent(this);
        jLabel1.getAccessibleContext().setAccessibleParent(this);
        txtNombres.getAccessibleContext().setAccessibleParent(this);
        jLabel3.getAccessibleContext().setAccessibleParent(this);
        txtDireccion.getAccessibleContext().setAccessibleParent(this);
        btnBuscarCliente.getAccessibleContext().setAccessibleParent(this);
        jLabel6.getAccessibleContext().setAccessibleParent(this);
        txtTelefono.getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        modelofac=new DefaultTableModel();
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        modelofac=new DefaultTableModel();
    }//GEN-LAST:event_formWindowClosed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.btncalcularActionPerformed(evt);

        if(modelofac.getRowCount()==0) JOptionPane.showMessageDialog(null,"Debe agregar productos a la compra","ERROR",JOptionPane.ERROR_MESSAGE);
        else
        {
            fecha = new Date();
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            fechafac=sd.format(fecha);

            objfac.setSubtotal(Float.parseFloat(this.txtSubtotal.getText()));
            objfac.setDescuento(descuento);
            objfac.setIva(Float.parseFloat(this.txtIva.getText()));
            objfac.setTotal(Float.parseFloat(this.txtTotal.getText()));
            objfac.setFecha(fechafac);
            objfac.setCliente(objcli);    

            obj_ln.guardar(objfac);    

            objcomp.setFactura(objfac);
            for (int i = 0; i <this.modelofac.getRowCount() ; i++){                
                objcomp.setCantidadcompra(Integer.parseInt(String.valueOf(this.modelofac.getValueAt(i, 1))));
                objcomp.setPrecioventaprod(Double.parseDouble(String.valueOf(this.modelofac.getValueAt(i, 3))));
                objcomp.setSubtotal(Double.parseDouble(String.valueOf(this.modelofac.getValueAt(i, 4))));
                //para mejor uso de memoria crear un objeto producto
                objcomp.setProducto(new PRODUCTO(Integer.parseInt(String.valueOf(this.modelofac.getValueAt(i, 0)))));               
                obj_ln.guardar(objcomp);  
                objcomp.setCodigo(0);
            }
            ERROR er = new ERROR();

            JasperViewer jviewer=new JasperViewer(obj_ln.parametros("factura.jasper",er,objfac),false);
            if(er.getTipo_error()==null)
            {           
                jviewer.setTitle("FACTURA");         
                jviewer.setVisible(true);
            }
            else
            {
                LOG l=new LOG();                                 
                String log="Tipo: "+er.getTipo_error()+". Mensaje: "+er.getDescripcion_error();            
                l.escribir(log);                                                                                     
                JOptionPane.showMessageDialog(null,obj_ln.error_usuario(er),"ERROR",JOptionPane.ERROR_MESSAGE);
            }
        }        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void btnBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarClienteActionPerformed
        try{            
            objcli.setCicliente(this.txtCedula.getText());
            
            obj_ln.buscarclif(objcli);
            if(objcli.getNombrec()!=null)
            {
                this.txtCedula.setEditable(false);
                this.txtCedula.setText(objcli.getCicliente());
                this.txtNombres.setText(objcli.getNombrec()+" "+objcli.getApellidoc());
                this.txtTelefono.setText(objcli.getTelefonoc());
                this.txtDireccion.setText(objcli.getDireccionc());
            }
            else
            {
                IU_INGRESO_CLIENTES objingcli=new IU_INGRESO_CLIENTES();
                objingcli.getTxt_ci().setText(txtCedula.getText());
                objingcli.getTxt_ci().setEnabled(false);
                objingcli.show();
            }
        }
        catch(NullPointerException e)
         {
             LOG l=new LOG();
             JOptionPane.showMessageDialog(null,obj_ln.error_usuario(objcli.getError()),"ERROR",JOptionPane.ERROR_MESSAGE);
             String log="Tipo: "+objcli.getError().getTipo_error()+". Mensaje: "+objcli.getError().getDescripcion_error();
             l.escribir(log);                          
             this.dispose();             
         }      
        
    }//GEN-LAST:event_btnBuscarClienteActionPerformed

    private void txtCedulaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCedulaKeyTyped
        // TODO add your handling code here:
        char car = evt.getKeyChar();
        if((car<'0' || car>'9')) evt.consume();
    }//GEN-LAST:event_txtCedulaKeyTyped

    private void txtDescuentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDescuentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDescuentoActionPerformed

    private void btncalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncalcularActionPerformed
        // TODO add your handling code here:

        double subtotal=0;
        double iva=0;
        double total=0;
        if(!this.txtDescuento.getText().isEmpty())
        {
            descuento=Float.parseFloat(this.txtDescuento.getText());
        }
        for (int i = 0; i <this.modelofac.getRowCount() ; i++) {
            subtotal+=Double.parseDouble(String.valueOf(this.modelofac.getValueAt(i, 4)));
        }
        this.txtSubtotal.setText(String.valueOf(subtotal));
        subtotal-=descuento;
        iva=subtotal*ivaporc/100;
        this.txtIva.setText(String.valueOf(iva));
        total=subtotal+iva;
        this.txtTotal.setText(String.valueOf(total));
        this.txtDescuento.setText(String.valueOf(descuento));        
    }//GEN-LAST:event_btncalcularActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        try{
            ivaporc=Integer.parseInt(JOptionPane.showInputDialog(null,"PORCENTAJE DE IVA: ","IMPUESTO",JOptionPane.QUESTION_MESSAGE).toString());
            if(ivaporc>0){
                this.lbliva.setText("IVA "+ivaporc+"%:");
            }
            else
            {
                JOptionPane.showMessageDialog(null,"INGRESE UN NUMERO POSITIVO","ERROR",JOptionPane.ERROR_MESSAGE);
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null,"INGRESE UN NUMERO VALIDO","ERROR",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void brnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_brnAgregarActionPerformed
        if(this.txtNombres.getText().isEmpty())this.btnBuscarClienteActionPerformed(evt);
        IU_BUSCAR_PROD_DESCRIPCION objbuspro = new IU_BUSCAR_PROD_DESCRIPCION ();
        objbuspro.setVisible(true);
    }//GEN-LAST:event_brnAgregarActionPerformed

    public class Verificar_CedulaRuc extends InputVerifier {
        public boolean verify(JComponent editor) {
            if (editor instanceof JTextField)
            {
                String clave = ((JTextField)editor).getText();
                if(clave.length()== 10 || clave.length()== 13){                    
                    return true;
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "La cedula debe tener 10 numeros o el ruc 13 numeros","CI/RUC no Valido",JOptionPane.ERROR_MESSAGE);
                   //((JTextField)editor).setText("");
                    return false;
                }
            }
            return true;
        }
    }
                
    public class Verificar_solo_Decimal extends InputVerifier {    
        public boolean verify(JComponent editor) {
            if (editor instanceof JTextField)
            {
                String clave = ((JTextField)editor).getText();
                try
                {
                    Double.parseDouble(clave);
                    return true;
                }
                catch (Exception e)
                {
                    JOptionPane.showMessageDialog(null, "Ingrese un numero valido","Numero no Valido",JOptionPane.ERROR_MESSAGE);
                   ((JTextField)editor).setText("");
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IU_FACTURA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IU_FACTURA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IU_FACTURA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IU_FACTURA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IU_FACTURA().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JTable TablaProductos;
    private javax.swing.JButton brnAgregar;
    private javax.swing.JButton btnBuscarCliente;
    private javax.swing.JButton btncalcular;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lbliva;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtDescuento;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtIva;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtSubtotal;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables
}